package com.dongway.netcondition.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.dongway.core.service.BaseService;
import com.dongway.netcondition.dto.AssistItem;
import com.dongway.netcondition.dto.AutoCompleteItem;
import com.dongway.netcondition.dto.RegionDTO;

@Component
public class DevSelectHelper extends BaseService {

    private static boolean loaded = false;

    private static List<AutoCompleteItem> branches = null;

    private static Map<Integer, List<AutoCompleteItem>> roadMap = null;

    private static Map<Integer, List<AutoCompleteItem>> stationMap = null;


    @PostConstruct
    public void init() {
        load(true);
    }


    public synchronized void load(boolean force) {
        if (loaded && force) {
            return;
        }
        if (logger.isDebugEnabled()) {
            logger.info("--------start load region-------");
        }
        loaded = false;
        branches = getAllBranch();
        List<AssistItem> roads = getAllRoad();
        List<AssistItem> stations = getAllStation();
        stationMap = listToMap(stations);
        roadMap = listToMap(roads);
        loaded = true;
        if (logger.isDebugEnabled()) {
            logger.info("{}--------load region end-------", branches, roadMap, stationMap);
        }
    }


    public List<AutoCompleteItem> getAllBranch() {
        String sql = "select id, name from t_region_branch";
        return getDao().query(AutoCompleteItem.class, sql);
    }


    public List<AssistItem> getAllRoad() {
        String sql = "select id, name, branch_id as assist_id from t_road";
        return getDao().query(AssistItem.class, sql);
    }


    public List<AssistItem> getAllStation() {
        String sql = "select id, name, road_id as assist_id from t_toll_station";
        return getDao().query(AssistItem.class, sql);
    }


    public List<AutoCompleteItem> getRoadListByBranchId(Integer branchId) {
        String sql = "select id, name from t_road where branch_id=?";
        return getDao().query(AutoCompleteItem.class, sql, branchId);
    }


    public List<AutoCompleteItem> getStationListByRoadId(Integer roadId) {
        String sql = "select id, name from t_toll_station where road_id=?";
        return getDao().query(AutoCompleteItem.class, sql, roadId);
    }


    public List<RegionDTO> getRegionMapping() {
        List<RegionDTO> res = new ArrayList<>();
        List<AssistItem> stationItems = getAllStation();
        List<AssistItem> roadItems = getAllRoad();
        List<AutoCompleteItem> branchItems = getAllBranch();
        Map<Integer, List<AutoCompleteItem>> stationMap = listToMap(stationItems);
        Map<Integer, List<RegionDTO.RoadDTO>> roadMap = new HashMap<>();
        for (AssistItem assistItem : roadItems) {
            List<RegionDTO.RoadDTO> items = roadMap.get(assistItem.getAssistId());
            if (!roadMap.containsKey(assistItem.getAssistId())) {
                items = new ArrayList<>();
                roadMap.put(assistItem.getAssistId(), items);
            }
            RegionDTO.RoadDTO roadDTO = new RegionDTO.RoadDTO();
            roadDTO.setId(assistItem.getId());
            roadDTO.setName(assistItem.getName());
            roadDTO.setStations(stationMap.get(assistItem.getId()));
            items.add(roadDTO);
        }
        for (AutoCompleteItem autoCompleteItem : branchItems) {
            RegionDTO regionDTO = new RegionDTO();
            regionDTO.setId(autoCompleteItem.getId());
            regionDTO.setName(autoCompleteItem.getName());
            regionDTO.setRoads(roadMap.get(regionDTO.getId()));
            res.add(regionDTO);
        }
        return res;
    }


    private Map<Integer, List<AutoCompleteItem>> listToMap(List<AssistItem> list) {
        Map<Integer, List<AutoCompleteItem>> map = new HashMap<>();
        for (AssistItem assistItem : list) {
            List<AutoCompleteItem> items = map.get(assistItem.getAssistId());
            if (!map.containsKey(assistItem.getAssistId())) {
                items = new ArrayList<>();
                map.put(assistItem.getAssistId(), items);
            }
            AutoCompleteItem item = new AutoCompleteItem();
            BeanUtils.copyProperties(assistItem, item);
            items.add(item);
        }
        return map;
    }

}
